const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;

class SettingsDeviceHandler extends BaseHandler {
    get pattern() {
        return "/settings/device/:action";
    }

    doGET(request, response) {

    }

    doPOST(request, response) {
        if (!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请登录系统后再调用此接口！'
            });
        } else {

            var bodyData = request.body, params;
            params = {
                deviceId: bodyData.deviceId,
                deviceName: bodyData.deviceName,
                ip: bodyData.ip,
                port: bodyData.port,
                ipcId: bodyData.ipcId,
                storeId: bodyData.storeId,
                type: request.params.action
            };
            this.Controller.createDevice(params, (result) => {
                if (result.error) {
                    return response.json({
                        success: false,
                        error: result.error
                    });
                }
                response.json({
                    success: true,
                    result: result
                });
            })
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST)
    }
}

module.exports = SettingsDeviceHandler;